package com.geline.cloud.domain;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 接口返回数据格式 200正常，500失败，1未登录，2密码过期
 */
@Getter
@Setter
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    //成功标志
    private boolean success = true;
    private Integer code = 0;
    private String msg = "操作成功！";
    private T data;

    public Result() {
    }

    public static <T> Result<T> ok() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> ok(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(200);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> fail() {
        return fail(500, "操作失败！");
    }

    public static <T> Result<T> fail(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(500);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> fail(String msg) {
        return fail(500, msg);
    }

    public static <T> Result<T> fail(int code, String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 未登录 statusCode=401
     */
    public static Result<Object> unLogin() {
        return fail(401, "未登录或登录超时。请重新登录！");
    }

    /**
     * 密码已过期 statusCode=502
     */
    public static Result<Object> passwordExpired() {
        return fail(502, "密码已过期！");
    }
}
